package fecat.ui.contextpanel.position;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;

import javax.swing.ButtonGroup;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import fecat.entities.OpenPositionModel;
import fecat.entities.QuoteModel;
import fecat.services.data.DataService;
import fecat.services.data.IQuotePriceChangeListener;
import fecat.services.position.OpenPositionService;
import fecat.services.quote.QuoteService;
import fecat.services.resource.ResourceService;
import fecat.ui.contextpanel.order.RemoveOrderDialog;
import fecat.ui.controls.ExchangeSpinner;
import fecat.ui.dialog.BaseDialog;
import fecat.util.ControlOperator;
import fecat.util.StringParser;
public class ClosePotionDialog extends BaseDialog implements IQuotePriceChangeListener{
	private ButtonGroup buttonGroup = new ButtonGroup();
	private JComboBox cmbCurrency;
	private JComboBox cmbOrderId;
	private ExchangeSpinner spnMoveCount;
	private JTextField txtPrice;
	private JSpinner spnLots;
	private JRadioButton radSell;
	private JRadioButton radBuy;
	private boolean isChanged=false;
	public void setOrderId(String orderId){
		this.cmbOrderId.setSelectedItem(orderId);
		this.setOrderPrice();
	}
	public String getOrderId(){
		return this.cmbOrderId.getSelectedItem().toString();
	}
	public String getOrderLots(){
		return this.spnLots.getValue().toString();
	}
	public String getClosePrice(){
		return this.txtPrice.getText();
	}
	public ClosePotionDialog(JFrame parent) {
		super(parent);
		setTitle(ResourceService.getString("Frame.Position.ClosePositon.Title"));
		setSize(new Dimension(270, 180));
		setResizable(false);
		getContentPane().setLayout(null);

		final JLabel label = new JLabel();
		label.setText(ResourceService.getString("Frame.Order.OrderNo")+":");
		label.setBounds(10, 10, 60, 15);
		getContentPane().add(label);

		final JLabel label_1 = new JLabel();
		label_1.setText(ResourceService.getString("Frame.Order.Currency")+":");
		label_1.setBounds(10, 31, 60, 15);
		getContentPane().add(label_1);

		final JLabel label_2 = new JLabel();
		label_2.setText(ResourceService.getString("Frame.Order.Sell/Buy"));
		label_2.setBounds(10, 65, 60, 15);
		getContentPane().add(label_2);

		radSell = new JRadioButton();
		buttonGroup.add(radSell);
		radSell.setText(ResourceService.getString("Frame.Order.Sell"));
		radSell.setBounds(76, 61, 58, 23);
		getContentPane().add(radSell);

		radBuy = new JRadioButton();
		buttonGroup.add(radBuy);
		radBuy.setText(ResourceService.getString("Frame.Order.Buy"));
		radBuy.setBounds(131, 61, 56, 23);
		getContentPane().add(radBuy);

		final JLabel label_3 = new JLabel();
		label_3.setText(ResourceService.getString("Frame.Order.Count")+":");
		label_3.setBounds(10, 91, 60, 15);
		getContentPane().add(label_3);

		spnLots = new JSpinner();
		spnLots.setBounds(73, 86, 102, 22);
		getContentPane().add(spnLots);

		final JLabel label_4 = new JLabel();
		label_4.setText(ResourceService.getString("Frame.Order.Price")+":");
		label_4.setBounds(10, 114, 60, 15);
		getContentPane().add(label_4);

		txtPrice = new JTextField();
		txtPrice.setFont(new Font("", Font.BOLD, 12));
		txtPrice.setText("12");
		txtPrice.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		txtPrice.setBounds(73, 111, 102, 21);
		txtPrice.setEnabled(false);
//		spnPrice.addChangeListener(new ChangeListener(){
//
//			public void stateChanged(ChangeEvent e) {
//				isChanged=true;
//			}
//			
//		});
		getContentPane().add(txtPrice);

//		final JLabel label_5 = new JLabel();
//		label_5.setText(ResourceService.getString("Frame.Position.MovingPoint")+":");
//		label_5.setBounds(10, 135, 60, 15);
//		getContentPane().add(label_5);
//
//		spnMoveCount = new ExchangeSpinner();
//		spnMoveCount.setBounds(73, 135, 102, 22);
//		spnMoveCount.setEnabled(false);
//		getContentPane().add(spnMoveCount);

		cmbOrderId = new JComboBox();
		cmbOrderId.setBounds(73, 7, 102, 21);
		cmbOrderId.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				setOrderPrice();
			}
			
		});
		getContentPane().add(cmbOrderId);

		cmbCurrency = new JComboBox();
		cmbCurrency.setBounds(73, 31, 102, 21);
		cmbCurrency.setEnabled(false);
		getContentPane().add(cmbCurrency);

		final JButton btnOK = new JButton();
		btnOK.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				enterOK();
				
			}
			
		});
		btnOK.setText(ResourceService.getString("Frame.Button.OK"));
		btnOK.setBounds(181, 6, 72, 23);
		getContentPane().add(btnOK);

		final JButton btnCancel = new JButton();
		btnCancel.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent arg0) {
				returnType=BaseDialog.CANCEL;
				ClosePotionDialog.this.dispose();
			}
			
		});
		btnCancel.setText(ResourceService.getString("Frame.Button.Cancel"));
		btnCancel.setBounds(181, 31, 72, 23);
		getContentPane().add(btnCancel);
		this.addOrders();
		this.setOrderPrice();
		getContentPane().addKeyListener(keyListener);
		DataService.addQuotePriceChangeListener(this);
		addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
            	btnOK.requestFocus();
            }
        });
		addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
            	btnOK.requestFocus();
            }
        });
	}
	private void enterOK(){
//		if(spnMoveCount.checkNum()){
			returnType=BaseDialog.OK;
			ClosePotionDialog.this.dispose();
//		}
	}
	private KeyListener keyListener=new KeyListener(){

		public void keyTyped(KeyEvent arg0) {
		}

		public void keyPressed(KeyEvent arg0) {
			 if(arg0.getKeyChar()==KeyEvent.VK_ENTER){
				 enterOK();
			 }
		}

		public void keyReleased(KeyEvent arg0) {
		}
		
	};
	private void addOrders(){
		OpenPositionModel[] opm=OpenPositionService.getOpenPositions();
		if(opm==null)
			return;
		for(int i=0;i<opm.length;i++){
			this.cmbOrderId.addItem(opm[i].getTicketId());
		}
	}
	public void dispose(){
		DataService.removeQuotePriceChangeListener(this);
		super.dispose();
	}
	private void setOrderPrice(){
		Object obj=this.cmbOrderId.getSelectedItem();
		if(obj==null)
			return;
		this.isChanged=false;
		OpenPositionModel opm=OpenPositionService.getOpenPositionByTicketId(String.valueOf(obj));
		if(opm!=null){
			this.cmbCurrency.addItem(opm.getCurrency());
			this.cmbCurrency.setSelectedItem(opm.getCurrency());
			ControlOperator.processOrderLotsSpinnerModel(this.spnLots,opm.getAccountId(),opm.getLeftLots());
			this.spnLots.setValue(opm.getLeftLots());
			
//			String price=String.valueOf(opm.getOpenPrice());
//			txtPrice.setText(price);
//			ControlOperator.processSpinnerModelValue(this.spnPrice,opm.getCurrency(),quoteValue);
			if(opm.getBuySellStatue().equals("B")){
				this.radSell.setSelected(true);
				this.radBuy.setEnabled(false);
				this.radSell.setEnabled(true);
			}
			else{
				this.radBuy.setSelected(true);
				this.radBuy.setEnabled(true);
				this.radSell.setEnabled(false);
			}
			QuoteModel qm=QuoteService.getNewQuoteModel(opm.getCurrency());
			if(qm!=null){
				if(this.radBuy.isSelected()){
					String price=String.valueOf(qm.getProperty("E"));
					txtPrice.setText(price);
				}
				else{
					String price=String.valueOf(qm.getProperty("D"));
					txtPrice.setText(price);
				}
			}
			if(opm.getIsLock().equals("2") || opm.getIsLock().equals("1")){
				this.spnLots.setEnabled(false);
			}
			else{
				this.spnLots.setEnabled(true);
			}
		}
	}
	public void quotePriceChanged(QuoteModel[] qms) {
		if(qms==null)
			return;
		if(isChanged)
			return;
		String currency=this.cmbCurrency.getSelectedItem().toString();
		QuoteModel qm=QuoteService.getNewQuoteModel(currency);
		if(qm!=null){
			if(this.radBuy.isSelected()){
				String price=String.valueOf(qm.getProperty("E"));
				txtPrice.setText(price);
//				double quoteValue=Double.parseDouble(price);
//				ControlOperator.processSpinnerModelValue(this.spnPrice,qm.getName(),quoteValue);
			}
			else{
				String price=String.valueOf(qm.getProperty("D"));
				txtPrice.setText(price);
//				double quoteValue=Double.parseDouble(price);
//				ControlOperator.processSpinnerModelValue(this.spnPrice,qm.getName(),quoteValue);
			}
		}
	}
}
